package ywpt.web.action.system;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.util.WebUtils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import ywpt.components.acegi.domain.Operation;
import ywpt.components.acegi.domain.Role;
import ywpt.core.web.BaseAction;
import ywpt.service.system.OperationService;
import ywpt.service.system.UserService;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class OperationAction extends BaseAction implements ModelDriven<Operation>, Preparable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -7465908738008777910L;
	
	private Operation operation;
	private OperationService operationService;
	private UserService			userService;
	private String	id;
	protected final Log	log= LogFactory.getLog(OperationAction.class);
	private List<Operation>	operationList		= new ArrayList<Operation>();
	private List<Long>		selectedRoles		= new ArrayList<Long>();
	
	public Operation getOperation() {
		return operation;
	}

	public void setOperation(Operation operation) {
		this.operation = operation;
	}

	public OperationService getOperationService() {
		return operationService;
	}

	public void setOperationService(OperationService operationService) {
		this.operationService = operationService;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public List<Operation> getOperationList() {
		return operationList;
	}

	public void setOperationList(List<Operation> operationList) {
		this.operationList = operationList;
	}

	public List<Long> getSelectedRoles() {
		return selectedRoles;
	}

	public void setSelectedRoles(List<Long> selectedRoles) {
		this.selectedRoles = selectedRoles;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}	
	
	@SuppressWarnings("unchecked")
	public String operTree() {
		JSONArray jsonArray = null;
		HttpServletResponse response = this.getResponse();
		response.setHeader("Content-Type", "text/html; charset=utf-8");
		PrintWriter out = null;
		List roles = new ArrayList();
		for (Iterator itr = userService.getUserByUsername(this.getCurrentUserName()).getRoles().iterator(); itr.hasNext();) {
			Role r = (Role) itr.next();
			roles.add(r);
		}
		try {
			out = response.getWriter();
			List<Map<String, Object>> children = new ArrayList<Map<String, Object>>();
			children.addAll(operationService.getOperationTree(1L));
			jsonArray = JSONArray.fromObject(children);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		out.write(WebUtils.toString(jsonArray));
		return NONE;
	}
	

	public String addRoles()
	{
		if (id != null) 
			this.operation = operationService.getOperationById(new Long(id));
		return INPUT;
	}
	
	public String saveRoles()
	{
		if (selectedRoles != null && selectedRoles.size() > 0) {
			operationService.addRoles(operation, selectedRoles);
		} else {
			this.addActionMessage("请选择要分配的操作权限!");
			return INPUT;
		}
		return SUCCESS;
	}
	
	
	@Override
	public void prepare() throws Exception {
		if (id != null) {
			this.operation = operationService.getOperationById(new Long(id));
		} else {
			this.operation = new Operation();
		}
	}
	
	@Override
	public Operation getModel() {
		return operation;
	}
/*	
	public String edit() {
		return INPUT;
	}
	
	public String view() {
		return "view";
	}
	
	public String input()
	{
		return INPUT;
	}
*/

}
